
// 根据本地uid 判断页面的显示隐藏
let Yongh = document.querySelector(".Yongh")
let Dengl = document.querySelector(".Dengl")
var bakeTuElement = document.querySelector('.bake_Tu');
console.log(bakeTuElement);

if (!localStorage.getItem("uid")) {
  Yongh.style.display = "none"
  Dengl.style.display = "block"
  bakeTuElement.style.display = "none"
} else {
  Yongh.style.display = "block"
  Dengl.style.display = "none"
  bakeTuElement.style.display = "block"
}

// 点击退出账号
function bakeHander() {
  localStorage.removeItem('uid');
  location.reload();
}

// 点击跳转购物车
function GoHander() {
  console.log("点击了");
  //检查本地存储是否存在uid
  if (localStorage.getItem('uid')) {

    window.location.href = "./05-购物车.html"
  } else {
    layui.use(function () {
      layui.layer.msg("没登录")
    })
  }
}


//获取所有的li元素
var liElements = document.querySelectorAll('.shang_imgs li');
var imgs = document.querySelectorAll(".shang_imgs li img")


for (var i = 0; i < liElements.length; i++) {
  // 鼠标移入事件
  liElements[i].addEventListener('mouseenter', function () {
    var imgSrc = this.querySelector('img').src;
    var huaDongElement = document.querySelector('.hua_dong');
    let titleImg = huaDongElement.querySelector("img")
    huaDongElement.style.backgroundImage = 'url(' + imgSrc + ')';
    huaDongElement.style.backgroundSize = 'cover'; // 设置背景图片充满容器
    huaDongElement.style.backgroundRepeat = "no-repeat"; /* 防止背景图片重复 */
    huaDongElement.style.backgroundPosition = "center"; /* 将背景图片居中显示 */
    huaDongElement.style.height = "450px"
    titleImg.style.display = "none"
  });

  //鼠标移出事件
  liElements[i].addEventListener('mouseleave', function () {
    var huaDongElement = document.querySelector('.hua_dong');
    let titleImg = huaDongElement.querySelector("img")
    huaDongElement.style.backgroundImage = ""
    titleImg.style.display = "block"
  });
}


// 获取详情信息
let params = new URLSearchParams(location.search)
let pid = params.get("pid")
// console.log(pid);
axiosGet("/api/front/articles/" + pid).then((r) => {
  console.log(r.data.data);
  let info = r.data.data
  let str = `
        <img src="${info.pict_url}" alt="">
      `
  document.querySelector(".hua_dong").innerHTML = str;

  let str1 = `
          <h1>${info.title}</h1>
          <span>${info.month_sale}</span>
        `
  document.querySelector(".main_title").innerHTML = str1;

  let str2 = `
          <span>感恩回馈</span>
          <span>￥</span>
          <span>${info.real_wap_price}</span>
          <span>起</span>
          <span class="layui-icon">前往峡谷查看更多鹏飞&#xe602;</span>
        `
  document.querySelector(".main_Jiaqian").innerHTML = str2;

})




// 加入购物车
let cartAPI = 'http://localhost:3000/api/front/carts'
let token = localStorage.getItem('token')
console.log(token);

cartAddHandler = function () {

  axios.defaults.headers.common['Authorization'] = "bearer " + token;
  console.log(token);
  let pid = params.get("pid")
  let pnum = document.querySelector(".pnum").value
  console.log(pid, pnum);
  axios.post(cartAPI, { pid, pnum: pnum }).then((res) => {
    console.log(res);
    layui.use(function () {
      layui.layer.msg(res.data.msg)
    })
  })
}

